package com.laicunba.ext.web.controller;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.laicunba.constant.AttributeConstant;
import com.laicunba.constant.GloabConstant;
import com.laicunba.pojo.ManagerUserDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.AlertService;
import com.laicunba.service.ManagerUserService;
import com.laicunba.service.StatusService;
import com.laicunba.service.UserService;
import com.laicunba.service.mq.ActiveMQService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.SessionUtil;
import com.laicunba.util.StringUtil;
import com.laicunba.util.ThreadUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class BaseController implements ApplicationEventPublisherAware {
	@Autowired
	private ManagerUserService managerUserService;
	@Autowired
	private UserService userService;
	@Autowired
	protected StatusService statusService;
	@Autowired
	protected AlertService alertService;
	
	
	@Autowired
	protected ActiveMQService activeMQService;
	
	
	private static JsonConfig jsonConfig = new JsonConfig();
	static {
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
				Date date = (Date) arg1;
				return date != null ? date.getTime() : null;
			}

			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});

		jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonValueProcessor() {
			public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
				Timestamp date = (Timestamp) arg1;
				return date != null ? date.getTime() : null;
			}

			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});
	}

	public String toJsonArray(Object o) {
		return JSONArray.fromObject(o, jsonConfig).toString();
	}

	public String toJsonObject(Object o) {
		return JSONObject.fromObject(o, jsonConfig).toString();
	}

	public JSONObject toJson(Object o) {
		return JSONObject.fromObject(o, jsonConfig);
	}

	/**
	 * 没有足够的参数
	 * 
	 * @param mobile
	 * @return
	 */
	protected boolean notHasEnoughParameter(Object... params) {
		for (Object obj : params) {
			if (obj == null) {
				return true;
			}
			if (obj instanceof String && StringUtil.isEmpty((String) obj)) {
				return true;
			}
		}
		return false;
	}

	protected boolean optPwdIsRight(String pwd) {
		return managerUserService.get(ManagerUserDO.class, SessionUtil.CURRENT_USER_ID.get()).getLoginPwd().equalsIgnoreCase(pwd);
	}

	protected boolean checkWithdrawConfirmPwdIsRight(String pwd) {
		return GloabConstant.SYS_WITHDRAW_CONFIRM_PWD.equalsIgnoreCase(pwd);
	}

	protected String getUserIdByAppToken(String token) {
		return CacheUtil.getAttribute(AttributeConstant.USER_LOGIN_TOKEN + token);
	}

	protected UserDO getUserByAppToken(String token) {
		String userId = getUserIdByAppToken(token);
		if (StringUtil.isEmpty(userId)) {
			return null;
		}
		return userService.get(UserDO.class, userId);
	}

	protected ApplicationEventPublisher applicationEventPublisher;

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;
	}

	/**
	 * 投送一个异步的消息
	 * 
	 * @param event
	 */
	protected void postEvent(final ApplicationEvent event) {
		ThreadUtil.execute(new Runnable() {

			@Override
			public void run() {
				applicationEventPublisher.publishEvent(event);
			}
		});
	}
	
	/**
	 * 获得请求
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}
}
